﻿using System;
using System.Collections.Generic;
using System.Text;

using Htss.Platform.Data;

namespace DreamBox.DAL.Posts
{
    public class Reply
    {
        /// <summary>
        /// 更新回帖锁定状态
        /// </summary>
        /// <param name="PostsID"></param>
        /// <param name="IsEnable"></param>
        /// <returns></returns>
        public bool UpdateStatus(int PostsID, bool IsEnable)
        {
            DbAction action = new DbAction("dbox_PostsReply");
            action.SetFields("ReplyID", PostsID, DbFieldType.Condition);
            action.SetFields("IsStatus", IsEnable);
            return action.Update();
        }
        /// <summary>
        /// 获取用户回复过的文字
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public string GetMyReplySql(string UserID)
        {
            StringBuilder str = new StringBuilder();
            str.Append("(select u.UserID,u.UserName,t.PostsID,t.Title,t.BrowseCount,t.Plus,t.ReplyCount,t.PostDate");
            str.Append("  from (select distinct PostsID from dbox_PostsReply where UserID="+UserID+") as p inner join ");
            str.Append("  dbox_Posts as t  on p.PostsID=t.PostsID inner join dbox_User as u on t.UserID=u.UserID ) as tmpTbl");
            return str.ToString();
        }
        /// <summary>
        /// 更新Plus次数
        /// </summary>
        /// <param name="PostsID"></param>
        /// <param name="Count"></param>
        /// <returns></returns>
        public bool UpdatePlus(int ReplyID)
        {
            DbAction action = new DbAction();
            action.SetFields("ReplyID", ReplyID);
            return action.ExecuteSqlNonQuery("update dbox_PostsReply set Plus=Plus+1 where ReplyID=@ReplyID");
        }
        /// <summary>
        /// 更新回复次数
        /// </summary>
        /// <param name="PostsID"></param>
        /// <param name="PostsCount"></param>
        /// <returns></returns>
        public bool UpdatePostsCount(int PostsID)
        {
            DbAction action = new DbAction();
            action.SetFields("PostsID", PostsID);
            return action.ExecuteSqlNonQuery("update dbox_Posts set ReplyCount=ReplyCount+1 where PostsID=@PostsID");
        }
        /// <summary>
        /// 发布回应记录
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool Add(DreamBox.Model.PostsReply m)
        {
            DbAction action = new DbAction("dbox_PostsReply");
            action.SetFields("PostsID", m.PostsID);
            action.SetFields("Contents",m.Contents);
            action.SetFields("UserID",m.UserID);
            action.SetFields("PostDate", DateTime.Now);
            action.SetFields("Plus",0);
            action.SetFields("IsStatus", true);
            return action.Insert();
        }
        /// <summary>
        /// 删除回复
        /// </summary>
        /// <param name="ReplyID"></param>
        /// <returns></returns>
        public bool Delete(int ReplyID)
        {
            DbAction action = new DbAction("dbox_PostsReply");
            action.SetFields("ReplyID", ReplyID, DbFieldType.Condition);
            return action.Delete();
        }
        /// <summary>
        /// 根据文字ID删除回帖
        /// </summary>
        /// <param name="ReplyID"></param>
        /// <returns></returns>
        public bool DeleteForPostsID(int PostsID)
        {
            DbAction action = new DbAction("dbox_PostsReply");
            return action.Delete("PostsID=" + PostsID);
        }
    }
}
